package com.atguigu.gmall.order.config;

import com.atguigu.gmall.common.constant.MqConst;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class MqConfig {

    @Bean
    public Exchange orderEventExchange() {
        /**
         * name:交换机名
         * durable：是否持久化
         * autoDelete：是否自动删除
         * arguments：其他配置项
         * "order-event-exchange"
         */
        return new TopicExchange(MqConst.ORDER_EVENT_EXCHANGE, true, false, null);

    }

    /**
     *
     * 延迟队列
     * exclusive:是否排他：有一个队列，在分布式的集群中，只有一个微服务可以进行监听
     * arguments:参数设置
     *
     * @return
     */
    @Bean
    public Queue orderDelayQueue() {
        HashMap<String, Object> arguments = new HashMap<>();
        //配置死信交换机
        arguments.put("x-dead-letter-exchange", MqConst.ORDER_EVENT_EXCHANGE);
        //配置过期走的路由键
        arguments.put("x-dead-letter-routing-key", MqConst.ORDER_TIMEOUT_RK);
        // 配置延迟时间
        arguments.put("x-message-ttl", MqConst.ORDER_TTL);
        return new Queue(MqConst.ORDER_DELAY_QUEUE, true, false, false,
                arguments);
    }

    @Bean
    public Binding delayBinding() {
        /**
         * String destination,
         * DestinationType destinationType,
         * String exchange,
         * String routingKey,
         * @Nullable Map<String, Object> arguments
         * exchange 指定的交换机和 destinationType类型指定的目的地，使用routingKey进行保定
         */
        Binding binding = new Binding(MqConst.ORDER_DELAY_QUEUE, Binding.DestinationType.QUEUE, MqConst.ORDER_EVENT_EXCHANGE,  MqConst.ORDER_CREATE_RK ,
                null);
        return binding;
    }

    @Bean //死信队列
    public Queue orderDeadQueue(){
        Queue queue = new Queue(MqConst.ORDER_DEAD_QUEUE,true,false, false,
        null);
        return queue;
    }

    @Bean
    public Binding deadBinding() {
        /**
         * String destination,
         * DestinationType destinationType,
         * String exchange,
         * String routingKey,
         * @Nullable Map<String, Object> arguments
         * exchange 指定的交换机和 destinationType类型指定的目的地，使用routingKey进行保定
         */
        Binding binding = new Binding(MqConst.ORDER_DEAD_QUEUE, Binding.DestinationType.QUEUE, MqConst.ORDER_EVENT_EXCHANGE, MqConst.ORDER_TIMEOUT_RK,
                null);
        return binding;
    }

    /**
     * 成功单队列
     * @return
     */
    @Bean
    public Queue payedQueue(){
        return new Queue(MqConst.ORDER_PAYED_QUEUE,true,false,false);
    }


    /**
     * 绑定队列
     * @return
     */
    @Bean
    public Binding payedBinding(){
        return new Binding(MqConst.ORDER_PAYED_QUEUE,
                Binding.DestinationType.QUEUE,
                MqConst.ORDER_EVENT_EXCHANGE,
                MqConst.ORDER_PAYED_RK,
                null);
    }
}
